﻿using System.Web.Mvc;
using System.Web.Security;
using usedcartrade.Models;
using usedcartrade.ViewModels;
using usedcartrade.Filter;
using usedcartrade.Validation;

namespace usedcartrade.Controllers
{
    public class AuthenticationController : Controller
    {
        // GET: Authentication
        public ActionResult Login()
        {
            LogRegUserViewModel lr = new LogRegUserViewModel();
            return View(lr);
        }

        public ActionResult Register()
        {
            LogRegUserViewModel registerUserViewModel = new LogRegUserViewModel();
            return View(registerUserViewModel);
        }

        [HttpPost]
        public ActionResult DoLogin(LogRegUserViewModel u,string BtnSubmit)
        {
            switch (BtnSubmit)
            {
                case "登录":
                    {
                        if (u.ValidCode != Session["ValidateCode"].ToString())
                        {
                            ModelState.AddModelError("", "验证码不正确.");
                            return RedirectToAction("Index","Home");
                        }
                        if (ModelState.IsValid)
                        {
                            UserBusinessLayer ubl = new UserBusinessLayer();
                            UserStatus status = ubl.GetUserValidity(ubl.GetUserDetails(u.UserName,u.PassWord));
                            bool IsAdmin = false;
                            if (status == UserStatus.AuthenticatedAdmin)
                            {
                                IsAdmin = true;
                            }
                            else if (status == UserStatus.AuthenticatedUser)
                            {
                                IsAdmin = false;
                            }
                            else
                            {
                                ModelState.AddModelError("CredentialError", "错误的用户名或密码");
                                return View("Login");
                            }
                            FormsAuthentication.SetAuthCookie(u.NickName, false);
                            Session["IsAdmin"] = IsAdmin;
                            Session["IsLogin"] = true;
                            Session["User"] = u;
                            return RedirectToAction("Index","Home");
                        }
                        else
                        {
                            return View("Login");
                        }
                    }
                case "注册":
                    {
                        return RedirectToAction("Register");
                    }
            }
            return new EmptyResult();
        }

        public ActionResult DoRegister(User u)
        {
            if (ModelState.IsValid
                    && u.userDetails.UserName != null
                    && u.userDetails.PassWord != null
                    && u.userDetails.Phone != null
                    && u.userDetails.City != null
                    && u.userDetails.Email != null)
            {
                u.userDetails.userlevel = 1;
                UserBusinessLayer usrBal = new UserBusinessLayer();
                if (!usrBal.ifuserexist(u))
                {
                    usrBal.SaveUser(u);
                    //return RedirectToAction("Index","Home");
                    return View("RegisterSuccess");
                }
            }
            LogRegUserViewModel vm = new LogRegUserViewModel();
            vm.UserName = u.userDetails.UserName;
            vm.City = "注册失败，请重新注册";
            return View("Register", vm);
        }


        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            Session["IsLogin"] = false;
            return RedirectToAction("Index","Home");
        }

        public ActionResult GetValidateCode()
        {
            ValidateCode vCode = new ValidateCode();
            string code = vCode.CreateValidateCode(4);
            Session["ValidateCode"] = code;
            byte[] bytes = vCode.CreateValidateGraphic(code);
            return File(bytes, @"image/jpeg");
        }

    }
}